<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BaseApiFunctionality
 *
 * @author Rakesh Srivastava
 */
class BaseApiFunctionality {

    static $format = 'json';

    public function validPostRequest() {
		
        if (!Yii::app()->getRequest()->getIsPostRequest()) {
        	$data = new ECOMResult(404, 'Invalid request method. Please check settings and try again.');
            ECOMApiResponder::Respond($data, date('m-d-Y'), self::$format);
        } else {
        	return self::processRequest();
        }
    }

    public function validGetRequest() {
        return self::processRequest();
    }

    private static function processRequest() {
        if (self::isValidDeviceId()) {
            if (self::isValidXAppKey()) {
                return true;
            }
        }
        return false;
    }

    private static function isValidDeviceId() {
        if (!ECOMAuthenticate::getObject()->isValidDeviceID()) {
            $data = new ECOMResult(401, "Invalid device Id. Please check settings and try again.");
            ECOMApiResponder::Respond($data, date('m-d-Y'), self::$format);
            return false;
        }
        return True;
    }

    private static function isValidXAppKey() {
        if (!ECOMAuthenticate::getObject()->isValidAppKey()) {
            $data = new ECOMResult(401, "Invalid app key. Please check settings and try again.");
            ECOMApiResponder::Respond($data, date('m-d-Y'), self::$format);
            return false;
        }
        return True;
    }

    public function isValidXAuthKey() {
        if (!ECOMAuthenticate::getObject()->isValidAuthKey()) {
            $data = new ECOMResult(401, "Invalid auth key. Please check settings and try again.");
            ECOMApiResponder::Respond($data, date('m-d-Y'), self::$format);
            return true;
        }
        return True;
    }

   

    public function getLogedInUser() {
        if ($this->isValidXAuthKey()) {
            $user_id = ECOMAuthenticate::getObject()->getUserByAuthKey();
            if ($user_id) {
                return (int)$user_id;
            } else {
                $data = new ECOMResult(401, "Invalid auth key. Please check settings and try again.");
                ECOMApiResponder::Respond($data, date('m-d-Y'), 'json');
            }
        }
       
    }

}

?>
